package com.peach.service.DeviceManage;

import com.peach.domain.DeviceManage.Device;
import com.peach.domain.DeviceManage.MyApp;
import com.peach.utils.SqlConfig;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("DeviceApplication")
public class DeviceApplicationImpl implements DeviceApplication {
    private static SqlSessionTemplate sqlSessionTemplate = SqlConfig.sessionTemplateConfigure();

    public int insertDeviceWithUDID(Device device){
        return sqlSessionTemplate.insert("DeviceMapper.insertDeviceWithUDID",device);
    }

    public int updatePushMagicAndToken(Device device){
        return sqlSessionTemplate.update("DeviceMapper.updatePushMagicAndToken",device);
    }

    public int updateDeviceDetail(Device device){
        return sqlSessionTemplate.update("DeviceMapper.updateDeviceDetail",device);
    }

    public List<Device> selectDeviceWithUDID(Device device){
        return sqlSessionTemplate.selectList("DeviceMapper.selectDeviceWithUDID",device);
    }

    public List<Device> selectDeviceWithOwner(Device device){
        return sqlSessionTemplate.selectList("DeviceMapper.selectDeviceWithOwner",device);
    }

    public List<Device> selectAllDevice(){
        return sqlSessionTemplate.selectList("DeviceMapper.selectAllDevice");

    }

    public int deleteDeviceWithUDID(Device device){
        return sqlSessionTemplate.delete("DeviceMapper.deleteDeviceWithUDID",device);
    }
    public int cleanOldAppList(String udid){
        //好一点的做法是新数据和旧数据比较  新数据中有旧数据中没有的则为新添加的数据，反之则为已删除的数据。类比微信公众号粉丝同步。
        //暂时先用此方案，直接把旧的表清了，重新插入新的数据。
        System.out.println("删除了UDID为："+udid+"的应用");
        return sqlSessionTemplate.delete("DeviceMapper.cleanOldAppList",udid);
    }

    public int insertNewApp(MyApp myApp){
        return sqlSessionTemplate.insert("DeviceMapper.insertNewAppWithID",myApp);
    }

}
